package spark.network.protocol;

import spark.network.Util;

public class ClockAdjustNotice extends Base {
	public static final short TYPE = 0x06;
	private static final byte[] TYPE_BYTES = Util.makeBytes(TYPE);

	private long adjustment;
	private static final int ADJUSTMENT_OFFSET = Base.SIZE;
	private static final int ADJUSTMENT_LENGTH = Long.SIZE / 8;

	private static final int SIZE =
			Base.SIZE +
			ADJUSTMENT_LENGTH;

	public ClockAdjustNotice() {
		super(true, TYPE_BYTES);
	}

	public long getAdjustment() {
		return adjustment;
	}

	public void setAdjustment(long adjustment) {
		this.adjustment = adjustment;
	}

	@Override
	public int getSize() {
		return SIZE;
	}

	@Override
	public void fill(byte[] buffer) {
		super.fill(buffer);
		adjustment = Util.makeLong(buffer, ADJUSTMENT_OFFSET);
	}

	@Override
	public void getBytes(byte[] buffer) {
		super.getBytes(buffer);
		Util.arraycopy(Util.makeBytes(adjustment), 0, buffer, ADJUSTMENT_OFFSET);
	}


	public static ClockAdjustNotice create(byte[] buffer, long receiveTime) {
		ClockAdjustNotice packet = new ClockAdjustNotice();
		packet.fill(buffer);
		packet.setTimeStampReceive(receiveTime);
		return packet;
	}
}